<?php
require_once(ML_ROOT."/service.php");

function on_index()
{
	set_navigation("index");
}

function on_models()
{
	require_once(ML_DATA."/model.php");
	
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		$selections = $_POST[Protocol::SELECTIONS];
		DBConnector::connect();
		Model::delete($selections);
		DBConnector::close();
		header("Location: ".url("/admin/models.php"));
	}
	
	set_navigation("models");
	
	DBConnector::connect();
	$model_list = Model::get_list();
	DBConnector::close();
	
	set_view_data(Protocol::MODEL_LIST, $model_list);
}

function on_models_edit()
{
	require_once(ML_DATA."/model.php");
	require_once(ML_DATA."/photo.php");
	
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		DBConnector::connect();
		Model::update($_POST);
		DBConnector::close();
		header("Location: ".url("/admin/models.php"));
	}
	
	set_navigation("models");
	
	$photo_list = NULL;
	DBConnector::connect();
	if (isset($_GET[Protocol::ID])) {
		$id = $_GET[Protocol::ID];
		$model = Model::get($id);
		$photo_list = Photo::get_list_by_model_id($id);
	}
	elseif (isset($_GET[Protocol::USER_ID])) {
		$user_id = $_GET[Protocol::USER_ID];
		$model = Model::get_by_user_id($user_id);
		if (count($model) == 0) {
			$model = User::get($user_id);
			$model[Protocol::USER_ID] = $user_id;
			$model[Protocol::ID] = NULL;
		}
		else $photo_list = Photo::get_list_by_model_id($model[Protocol::ID]);
	}
	else ;
	DBConnector::close();
	
	set_view_data(Protocol::MODEL, $model);
	set_view_data(Protocol::PHOTO_LIST, $photo_list);
}

function on_photos()
{
	require_once(ML_DATA."/photo.php");
	
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		$selections = $_POST[Protocol::SELECTIONS];
		DBConnector::connect();
		if ($_POST[Protocol::SUBMIT] == Protocol::DELETE) Photo::delete($selections);
		else ;
		DBConnector::close();
		header("Location: ".url("/admin/photos.php"));
	}
	
	set_navigation("photos");
	
	DBConnector::connect();
	$photo_list = Photo::get_list();
	DBConnector::close();
	
	set_view_data(Protocol::PHOTO_LIST, $photo_list);
}

function on_photos_edit()
{
	require_once(ML_DATA."/photo.php");
	require_once(ML_DATA."/model.php");
	
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		DBConnector::connect();
		Photo::update($_POST);
		DBConnector::close();
		header("Location: ".url("/admin/photos.php"));
	}
	
	set_navigation("photos");

	DBConnector::connect();
	if (isset($_GET[Protocol::ID]))
		set_view_data(Protocol::PHOTO, Photo::get($_GET[Protocol::ID]));
	$model_list = Model::get_list();
	DBConnector::close();
	
	set_view_data(Protocol::MODEL_LIST, $model_list);
}

function on_users()
{
	require_once(ML_DATA."/user.php");
	
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		$selections = $_POST[Protocol::SELECTIONS];
		DBConnector::connect();
		if ($_POST[Protocol::SUBMIT] == Protocol::DELETE) User::delete($selections);
		else ;
		DBConnector::close();
		header("Location: ".url("/admin/users.php"));
	}
	
	set_navigation("users");
	
	DBConnector::connect();
	$user_list = User::get_list();
	DBConnector::close();
	
	set_view_data(Protocol::USER_LIST, $user_list);
}

function on_users_edit()
{
	require_once(ML_DATA."/user.php");
	
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		DBConnector::connect();
		User::update($_POST);
		DBConnector::close();
		header("Location: ".url("/admin/users.php"));
	}
	
	set_navigation("users");
	
	if (isset($_GET[Protocol::ID])) {
		DBConnector::connect();
		set_view_data(Protocol::USER, User::get($_GET[Protocol::ID]));
		DBConnector::close();
	}
}

function on_comments()
{
	require_once(ML_DATA."/comment.php");
	
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		$selections = $_POST[Protocol::SELECTIONS];
		DBConnector::connect();
		if ($_POST[Protocol::SUBMIT] == Protocol::DELETE)
			Comment::delete_model_comment($selections);
		else ;
		DBConnector::close();
		header("Location: ".url("/admin/comments.php"));
	}
	
	set_navigation("comments");
	
	DBConnector::connect();
	$comment_list = Comment::get_model_comment_list();
	DBConnector::close();
	
	set_view_data(Protocol::COMMENT_LIST, $comment_list);
}

function on_comments_edit()
{
	require_once(ML_DATA."/comment.php");
	require_once(ML_DATA."/user.php");
	require_once(ML_DATA."/model.php");
	
	if ($_SERVER["REQUEST_METHOD"] == "POST") {
		DBConnector::connect();
		Comment::update_model_comment($_POST);
		DBConnector::close();
		header("Location: ".url("/admin/comments.php"));
	}
	
	set_navigation("comments");
	
	DBConnector::connect();
	if (isset($_GET[Protocol::ID]))
		set_view_data(Protocol::COMMENT, Comment::get_model_comment($_GET[Protocol::ID]));
	$user_list = User::get_list();
	$target_list = Model::get_list();
	DBConnector::close();

	set_view_data(Protocol::USER_LIST, $user_list);
	set_view_data(Protocol::TARGET_LIST, $target_list);
}
